home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 147
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan).7z
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan) (Track 1).bin
/
tools
/
zmc3v078
/
zmc3v078.lzh
/
SRCSV078.LZH
/
SWITCH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-04-24
|
5KB
|
218 lines
/* ================
switch
================ */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "config.h"
#include "etc.h"
#include "68lib.h" /* strlwr */
#include "zmc3.h"
#define SET_VAL -2
#define RES_VAL -1
int getSwitch(int argc,char **argv);
int getSwitchVal(char letter);
void setSwitchVal(char letter,char num);
char *getInFile(void);
char *getOutFile(void);
char *setInFile(char *filename);
char *setOutFile(char *filename);
#ifndef WARNLVL
#define WARNLVL 3
#endif
const int getnum(char **ptr);
void swerr(void);
#ifndef UNIXY_OS
#ifndef X68000
#include <io.h>
#else
#include <unistd.h>
#endif
#endif
static WORD swtbl[26] = { RES_VAL,RES_VAL,RES_VAL,RES_VAL,RES_VAL,
/* A B C D E */
RES_VAL,RES_VAL,RES_VAL,RES_VAL,RES_VAL,
/* F G H I J */
RES_VAL,1, RES_VAL,RES_VAL,RES_VAL,
/* K L M N O */
RES_VAL,RES_VAL,RES_VAL,RES_VAL,RES_VAL,
/* P Q R S T */
RES_VAL,3 ,WARNLVL,RES_VAL,RES_VAL,1};
/* U V W X Y Z */
static char infile[2049], outfile[2049];
/* ==========================
get command line
========================== */
int getSwitch(int argc,char **argv)
{
infile[0] = outfile[0] = '\0';
if (argc == 1) {
#ifdef UNIXY_OS
strcpy(infile,"-");
setSwitchVal('c',SET_VAL);
#else
return 1;
#endif
}
while (--argc > 0) {
char opt[2049];
if ( strlen(*++argv) > 2048 ) {
fprintf( stderr,"Warning: too long switches.\n");
}
strcpy( opt,*argv );
#ifdef UNIXY_OS
if ( opt[0]=='-' ) {
#else
if ( opt[0]=='-' || opt[0]=='/' ) {
#endif
char *ptr = strlwr(opt);
ptr++;
if (!*ptr) { /* use standard input */
if (!infile[0]) {
infile[0] = '-';
infile[1] = '\0';
} else {
fatal(TOOMANYFILES,"Error: too many sources are specified.\n");
}
}
while (*ptr) {
char ch = *ptr;
if (ch == '?') {
help(0,0);
} else if (!isalpha(ch)) {
fatal(SWERR,"Error: %c: it's a invalid switch.\n",ch);
} else {
if (ch == 'o') { /* destination filename will be specified */
if (*++ptr) {
strcpy(outfile,ptr);
while (*ptr) ptr++;
} else if (--argc) {
strcpy(outfile,*++argv);
/* argv++; */
/* argc--; */
} else {
fatal(SWERR,"Error: -o: no ZMD file specified.\n");
}
break;
} else if (ch == 'e') { /* ERROR filename will be specified */
FILE *fp;
if (*++ptr) {
fp = freopen(ptr,"w",stderr);
if (fp == (FILE*)NULL) {
fatal(SWERR,"Error: -e: failed to redirect.\n");
}
while (*ptr) ptr++;
} else if (--argc) {
fp = freopen(*++argv,"w",stderr);
if (fp == (FILE*)NULL) {
fatal(SWERR,"Error: -e: failed to redirect.\n");
}
} else {
fatal(SWERR,"Error: -e: no output file specified.\n");
}
break;
} else if ( isdigit((int)*++ptr) ) {
setSwitchVal(ch, getnum(&ptr));
} else {
setSwitchVal(ch, SET_VAL);
}
}
}
} else {
if (!infile[0]) {
strcpy(infile,opt);
} else {
fatal(TOOMANYFILES,"Error: too many sources are specified.\n" );
}
}
}
return 0;
}
/* ==============================
pick up numerics
============================== */
const int getnum(char **ptr)
{
int n = 0;
if (!**ptr) {
swerr();
}
while ( isdigit((int)**ptr) ) {
n = n * 10 + (**ptr) - '0';
(*ptr)++;
}
if (n < 0) swerr();
return n;
}
/* ==================================
numeric err
================================== */
void swerr(void)
{
fprintf( stderr,"invalid number.\n" );
help(1,0);
}
/* ===================================
return specified switch's value
=================================== */
int getSwitchVal(char letter)
{
letter |= 0x20;
return swtbl[ letter - 'a' ];
}
void setSwitchVal(char letter,char num)
{
letter |= 0x20;
swtbl[ letter - 'a' ] = num;
}
char *getInFile(void)
{
return infile;
}
char *getOutFile(void)
{
return outfile;
}
char *setInFile(char *filename)
{
return strcpy(infile,filename);
}
char *setOutFile(char *filename)
{
return strcpy(outfile,filename);
}